import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;
import javax.swing.JFormattedTextField;
import javax.swing.JComboBox;


public class AnaPencere extends JFrame
{
	
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textFieldAd;
	private JTextField textFieldSoyad;
	private JTextField textFieldNo;
	private JLabel lblSnf;
	private JButton btnAra;
	private JButton btnEkle;
	private JScrollPane scrollPane;
	private JTable table = new JTable()
	{
		private static final long serialVersionUID = 1L;
		public boolean isCellEditable(int row, int column) {return false;};
	};
	private String[] tableColumnNames = {"Ad", "Soyad", "Numara", "Telefon", "Sinif"};
	private JButton btnBosalt;
	private DefaultTableModel tableModel1;
	private JButton btnSil;
	private JButton btnDzenle;
	private JButton btnGetir;
	private JLabel lblTel;
	private JFormattedTextField textFieldTelefon;
	private JButton btnTemizle;
	private JButton btnYardim;
	private JComboBox comboBoxSinif;

	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					AnaPencere frame = new AnaPencere();
					frame.setVisible(true);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AnaPencere()
	{
		setTitle("Topluluk Listesi");
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 585, 397);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		JLabel lblAd = new JLabel("Ad\u0131:");
		GridBagConstraints gbc_lblAd = new GridBagConstraints();
		gbc_lblAd.anchor = GridBagConstraints.WEST;
		gbc_lblAd.insets = new Insets(0, 0, 5, 5);
		gbc_lblAd.gridx = 0;
		gbc_lblAd.gridy = 0;
		contentPane.add(lblAd, gbc_lblAd);
		
		textFieldAd = new JTextField();
		GridBagConstraints gbc_textFieldAd = new GridBagConstraints();
		gbc_textFieldAd.gridwidth = 11;
		gbc_textFieldAd.insets = new Insets(0, 0, 5, 0);
		gbc_textFieldAd.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldAd.gridx = 2;
		gbc_textFieldAd.gridy = 0;
		contentPane.add(textFieldAd, gbc_textFieldAd);
		textFieldAd.setColumns(10);
		
		JLabel lblSoyad = new JLabel("Soyad\u0131:");
		GridBagConstraints gbc_lblSoyad = new GridBagConstraints();
		gbc_lblSoyad.anchor = GridBagConstraints.WEST;
		gbc_lblSoyad.insets = new Insets(0, 0, 5, 5);
		gbc_lblSoyad.gridx = 0;
		gbc_lblSoyad.gridy = 1;
		contentPane.add(lblSoyad, gbc_lblSoyad);
		
		textFieldSoyad = new JTextField();
		GridBagConstraints gbc_textFieldSoyad = new GridBagConstraints();
		gbc_textFieldSoyad.gridwidth = 11;
		gbc_textFieldSoyad.insets = new Insets(0, 0, 5, 0);
		gbc_textFieldSoyad.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldSoyad.gridx = 2;
		gbc_textFieldSoyad.gridy = 1;
		contentPane.add(textFieldSoyad, gbc_textFieldSoyad);
		textFieldSoyad.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("Numaras\u0131:");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 2;
		contentPane.add(lblNewLabel, gbc_lblNewLabel);
		
		textFieldNo = new JTextField();
		GridBagConstraints gbc_textFieldNo = new GridBagConstraints();
		gbc_textFieldNo.gridwidth = 3;
		gbc_textFieldNo.insets = new Insets(0, 0, 5, 5);
		gbc_textFieldNo.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldNo.gridx = 2;
		gbc_textFieldNo.gridy = 2;
		contentPane.add(textFieldNo, gbc_textFieldNo);
		textFieldNo.setColumns(10);
		
		lblTel = new JLabel("Tel:");
		GridBagConstraints gbc_lblTel = new GridBagConstraints();
		gbc_lblTel.anchor = GridBagConstraints.EAST;
		gbc_lblTel.insets = new Insets(0, 0, 5, 5);
		gbc_lblTel.gridx = 6;
		gbc_lblTel.gridy = 2;
		contentPane.add(lblTel, gbc_lblTel);
		
		
		textFieldTelefon = new JFormattedTextField(createFormatter("(***) ***-****"));
		GridBagConstraints gbc_formattedTextField = new GridBagConstraints();
		gbc_formattedTextField.gridwidth = 3;
		gbc_formattedTextField.insets = new Insets(0, 0, 5, 5);
		gbc_formattedTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_formattedTextField.gridx = 7;
		gbc_formattedTextField.gridy = 2;
		contentPane.add(textFieldTelefon, gbc_formattedTextField);
		
		
		lblSnf = new JLabel("S\u0131n\u0131f\u0131:");
		GridBagConstraints gbc_lblSnf = new GridBagConstraints();
		gbc_lblSnf.anchor = GridBagConstraints.EAST;
		gbc_lblSnf.insets = new Insets(0, 0, 5, 5);
		gbc_lblSnf.gridx = 10;
		gbc_lblSnf.gridy = 2;
		contentPane.add(lblSnf, gbc_lblSnf);
		
		String[] items = {"", "Haz\u0131rl\u0131k","1","2","3","4"};
		comboBoxSinif = new JComboBox(items);
		comboBoxSinif.setMaximumRowCount(6);
		comboBoxSinif.setToolTipText("");
		GridBagConstraints gbc_comboBoxSinif = new GridBagConstraints();
		gbc_comboBoxSinif.gridwidth = 2;
		gbc_comboBoxSinif.insets = new Insets(0, 0, 5, 0);
		gbc_comboBoxSinif.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBoxSinif.gridx = 11;
		gbc_comboBoxSinif.gridy = 2;
		contentPane.add(comboBoxSinif, gbc_comboBoxSinif);
				
		btnEkle = new JButton("Ekle");
		GridBagConstraints gbc_btnEkle = new GridBagConstraints();
		gbc_btnEkle.gridwidth = 3;
		gbc_btnEkle.insets = new Insets(0, 0, 5, 5);
		gbc_btnEkle.gridx = 0;
		gbc_btnEkle.gridy = 3;
		contentPane.add(btnEkle, gbc_btnEkle);
				
		btnAra = new JButton("Ara");
		GridBagConstraints gbc_btnAra = new GridBagConstraints();
		gbc_btnAra.gridwidth = 3;
		gbc_btnAra.insets = new Insets(0, 0, 5, 5);
		gbc_btnAra.gridx = 3;
		gbc_btnAra.gridy = 3;
		contentPane.add(btnAra, gbc_btnAra);
			
		btnDzenle = new JButton("D\u00FCzenle");
		GridBagConstraints gbc_btnDzenle = new GridBagConstraints();
		gbc_btnDzenle.gridwidth = 3;
		gbc_btnDzenle.insets = new Insets(0, 0, 5, 5);
		gbc_btnDzenle.gridx = 6;
		gbc_btnDzenle.gridy = 3;
		contentPane.add(btnDzenle, gbc_btnDzenle);
		
		btnSil = new JButton("Sil");
		GridBagConstraints gbc_btnSil = new GridBagConstraints();
		gbc_btnSil.gridwidth = 2;
		gbc_btnSil.insets = new Insets(0, 0, 5, 5);
		gbc_btnSil.gridx = 9;
		gbc_btnSil.gridy = 3;
		contentPane.add(btnSil, gbc_btnSil);
				
		btnGetir = new JButton("Getir");
		GridBagConstraints gbc_btnGetir = new GridBagConstraints();
		gbc_btnGetir.gridwidth = 2;
		gbc_btnGetir.insets = new Insets(0, 0, 5, 0);
		gbc_btnGetir.gridx = 11;
		gbc_btnGetir.gridy = 3;
		contentPane.add(btnGetir, gbc_btnGetir);
		
		scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 13;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 4;
		contentPane.add(scrollPane, gbc_scrollPane);
		scrollPane.setViewportView(table);
		
		btnYardim = new JButton("Yard\u0131m");
		GridBagConstraints gbc_btnYardim = new GridBagConstraints();
		gbc_btnYardim.insets = new Insets(0, 0, 0, 5);
		gbc_btnYardim.gridx = 0;
		gbc_btnYardim.gridy = 5;
		contentPane.add(btnYardim, gbc_btnYardim);
		
		btnTemizle = new JButton("Alanlar\u0131 Temizle");
		GridBagConstraints gbc_btnTemizle = new GridBagConstraints();
		gbc_btnTemizle.gridwidth = 3;
		gbc_btnTemizle.insets = new Insets(0, 0, 0, 5);
		gbc_btnTemizle.gridx = 7;
		gbc_btnTemizle.gridy = 5;
		contentPane.add(btnTemizle, gbc_btnTemizle);
				
		btnBosalt = new JButton("Tabloyu Bo\u015Falt");
		GridBagConstraints gbc_btnBosalt = new GridBagConstraints();
		gbc_btnBosalt.gridwidth = 3;
		gbc_btnBosalt.gridx = 10;
		gbc_btnBosalt.gridy = 5;
		contentPane.add(btnBosalt, gbc_btnBosalt);
		
		tableModel1 = (DefaultTableModel) table.getModel();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listeyiOlustur((new Baglanti()).listeVer());
		table.setModel(tableModel1);

		btnTemizle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				alanlariTemizle();
			}
		});

		btnEkle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ekle();
			}
		});
		
		btnAra.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ara();
			}
		});
		
		btnDzenle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				duzenle();
			}
		});
		
		btnSil.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sil();
			}	
		});

		btnGetir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getir();
			}
		});
		
		btnBosalt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				listeyiBosalt();
			}
		});

		btnYardim.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			
				Yardim y = new Yardim();
				y.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
				y.setVisible(true);
			}
		});
	}	

	protected void sil()
	{

		if(!textFieldNo.getText().isEmpty())
		{
			Baglanti db = new Baglanti();
			long silinecek = Long.parseLong(textFieldNo.getText());			
			if(silinecek <= 0)
				JOptionPane.showMessageDialog(this, "Bo\u015Fluklar yanl\u0131\u015F dolduruldu!", "Hata!", JOptionPane.ERROR_MESSAGE);
			else
			{
				if(JOptionPane.showConfirmDialog(this, silinecek + " numaral\u0131 kayd\u0131 silmek istedi\u011finize emin misiniz?", "Uyar\u0131", JOptionPane.CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == 0)
					db.sil(silinecek);
				
				listeyiBosalt();
				listeyiOlustur(db.listeVer());
			}
		}

	}
	protected void duzenle()
	{
		boolean durum = true;
		String kontrol[] = new String[5];
		
		kontrol[0] = textFieldNo.getText().trim();
		kontrol[1] = textFieldAd.getText().trim();
		kontrol[2] = textFieldSoyad.getText().trim();
		kontrol[3] = getUnformattedTelefon(textFieldTelefon.getText().trim());
		kontrol[4] = (String)comboBoxSinif.getSelectedItem();	
		
		if(!kontrol[0].isEmpty() && Long.parseLong(kontrol[0]) > 0)
			durum = true;
		else
			durum = false;
		
		for(int i = 0; i < 5; i++)
			if(kontrol[i].isEmpty())
				durum = false;

		if(durum)
		{
			Baglanti db = new Baglanti();

			Ogrenci degistir = new Ogrenci();
			degistir.setNo(Long.parseLong(kontrol[0]));
			degistir.setAd(kontrol[1]);
			degistir.setSoyad(kontrol[2]);
			degistir.setTelefon(kontrol[3]);
			degistir.setSinif(comboBoxSinif.getSelectedIndex() - 1);	//0-"", 1-"hazirlik", 2-"1" .... 
				
			db.degistir(degistir);
			
			listeyiBosalt();
			listeyiOlustur(db.listeVer());
		}
		else
			JOptionPane.showMessageDialog(this, "Bo\u015Fluklar yanl\u0131\u015F dolduruldu!", "Hata!", JOptionPane.ERROR_MESSAGE);
	}

	protected void getir()
	{
		boolean durum = false;
		for(int i = 0; i < table.getRowCount(); i++)
			if(table.isRowSelected(i))
				durum = true;
		if(durum)
		{
			textFieldAd.setText(tableModel1.getValueAt(table.getSelectedRow(), 0).toString());
			textFieldSoyad.setText(tableModel1.getValueAt(table.getSelectedRow(), 1).toString());
			textFieldNo.setText(tableModel1.getValueAt(table.getSelectedRow(), 2).toString());
			textFieldTelefon.setText(tableModel1.getValueAt(table.getSelectedRow(), 3).toString());
			comboBoxSinif.setSelectedItem((String)tableModel1.getValueAt(table.getSelectedRow(), 4).toString());
		}
	}

	protected void ekle()
	{
		boolean durum = true;
		String kontrol[] = new String[5];
		
		kontrol[0] = textFieldNo.getText().trim();
		kontrol[1] = textFieldAd.getText().trim();
		kontrol[2] = textFieldSoyad.getText().trim();
		kontrol[3] = getUnformattedTelefon(textFieldTelefon.getText().trim());
		kontrol[4] = (String)comboBoxSinif.getSelectedItem();
		
		if(!kontrol[0].isEmpty() && Long.parseLong(kontrol[0]) > 0)
			durum = true;
		else
			durum = false;
		
		for(int i = 0; i < 5; i++)
			if(kontrol[i].isEmpty())
				durum = false;


		if(durum)
		{
			Baglanti db = new Baglanti();

			Ogrenci eklenecek = new Ogrenci();
			eklenecek.setNo(Long.parseLong(kontrol[0]));
			eklenecek.setAd(kontrol[1]);
			eklenecek.setSoyad(kontrol[2]);
			eklenecek.setTelefon(kontrol[3]);
			eklenecek.setSinif(comboBoxSinif.getSelectedIndex() - 1);	//0-"", 1-"hazirlik", 2-"1" .... 
	
			db.ekle(eklenecek);
			
			listeyiBosalt();
			listeyiOlustur(db.listeVer());
		}
		else
			JOptionPane.showMessageDialog(this, "Bo\u015Fluklar yanl\u0131\u015F dolduruldu!", "Hata!", JOptionPane.ERROR_MESSAGE);	
	}

	protected void ara()
	{
		boolean durum = false;
		String kontrol[] = new String[5];
		
		kontrol[0] = textFieldNo.getText().trim();
		kontrol[1] = textFieldAd.getText().trim();
		kontrol[2] = textFieldSoyad.getText().trim();
		kontrol[3] = getUnformattedTelefon(textFieldTelefon.getText().trim());
		kontrol[4] = (String)comboBoxSinif.getSelectedItem();
		
		if(!kontrol[0].isEmpty() && Long.parseLong(kontrol[0]) > 0)
			durum = true;
		else
			kontrol[0] = "-1";
		
		for(int i = 1; i < 4; i++)
			if(!kontrol[i].isEmpty())
				durum = true;
			else
				kontrol[i] = "*";
		
		if(!kontrol[4].isEmpty())
			durum = true;
		else
			kontrol[4] = "-1";
		

		Baglanti db = new Baglanti();	

		if(durum)
		{
			Ogrenci aranacak = new Ogrenci();
			aranacak.setNo(Long.parseLong(kontrol[0]));
			aranacak.setAd(kontrol[1]);
			aranacak.setSoyad(kontrol[2]);
			aranacak.setTelefon(kontrol[3]);
			aranacak.setSinif(comboBoxSinif.getSelectedIndex() - 1);	//0-"", 1-"hazirlik", 2-"1" .... 
			
			listeyiBosalt();
			listeyiOlustur(db.ara(aranacak));
		}
		else
		{
			JOptionPane.showMessageDialog(this, "Bo\u015Fluklar yanl\u0131\u015F dolduruldu. T\u00fcm sonu\u00e7lar listeleniyor.", "Uyar\u0131!", JOptionPane.WARNING_MESSAGE);
			listeyiBosalt();
			listeyiOlustur(db.listeVer());
		}
	
	}

	protected void listeyiOlustur(Vector<Ogrenci> data)
	{
		String dataObject[][] = new String[data.size()][5];
		for(int i = 0; i < data.size(); i++)
		{
			dataObject[i][0] = data.elementAt(i).getAd();
			dataObject[i][1] = data.elementAt(i).getSoyad();
			dataObject[i][2] = Long.toString(data.elementAt(i).getNo());
			dataObject[i][3] = getformattedTelefon(data.elementAt(i).getTelefon());
			if(data.elementAt(i).getSinif() != 0)
				dataObject[i][4] = Integer.toString(data.elementAt(i).getSinif());
			else
				dataObject[i][4] = "Haz\u0131rl\u0131k";
		}
		
		tableModel1.setDataVector(dataObject, tableColumnNames);
	}
	
	protected void listeyiBosalt()
	{
		int k = tableModel1.getRowCount();
		for(int i = 0; i < k; i++)
			tableModel1.removeRow(0);
	}

	protected void alanlariTemizle()
	{
		textFieldAd.setText("");
		textFieldSoyad.setText("");
		textFieldNo.setText("");
		textFieldTelefon.setText("");
		comboBoxSinif.setSelectedIndex(0);
		
	}
	
	public String getformattedTelefon(String input)
	{
		String ret = "";
		ret += "(";
		byte k = 0;
		for(int i = 0; i < 3; i++, k++)
			ret += input.charAt(k);
		ret += ")";
		for(int i = 0; i < 3; i++, k++)
			ret += input.charAt(k);
		ret += "-";
		for(int i = 0; i < 4; i++, k++)
			ret += input.charAt(k);
		
		return ret;
	}
	
	public  String getUnformattedTelefon(String formattedTelefon)
	{
		String ret = "";
		for(int i = 0; i < formattedTelefon.length(); i++)
			if(Character.isDigit(formattedTelefon.charAt(i)) || formattedTelefon.charAt(i) == '_')
				ret += formattedTelefon.charAt(i);
		return (ret.equals("__________") ? "" : ret);
	}
	
	protected MaskFormatter createFormatter(String s)
	{
		MaskFormatter formatter = null;
		
		try
		{
			formatter = new MaskFormatter(s);
			formatter.setPlaceholderCharacter('_');
			formatter.setValidCharacters("1234567890_");
		} catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return formatter;
	}

}
